package com.argticling.dao.impl;

import com.argticling.dao.impl.hibernate.HibernateUtil;
import com.argticling.dao.interf.ContactDAO;
import com.argticling.model.chanel.Contact;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.List;

public class ContactDAOImplementation extends AbstractDAOImplementation implements ContactDAO {
    public List<Contact> getContacts(String type) {
        List<Contact> contactList = new ArrayList<Contact>();
        HibernateUtil hibernateUtil = new HibernateUtil();
        Session session = hibernateUtil.getSession();
        try {
            String query = "from ContactImpl where type ='" + type + "' ";
            contactList = session.createQuery(query).list();
        } finally {
            session.close();
        }
        return contactList;
    }

    public List<Contact> getUserContacts(String userId) {
        List<Contact> contactList = new ArrayList<Contact>();
        Session session = new HibernateUtil().getSession();
        try {
            String query = "from ContactImpl where userId ='" + userId + "' ";
            contactList = session.createQuery(query).list();
        } finally {
            session.close();
        }
        return contactList;
    }

    public List<Contact> getContactsByType(String type) {
        List<Contact> contactList = new ArrayList<Contact>();
        Session session = new HibernateUtil().getSession();
        try {
            String query = "from ContactImpl where type ='" + type + "' ";
            contactList = session.createQuery(query).list();
        } finally {
            session.close();
        }
        return contactList;
    }

    public Contact getContactById(String id) {
        List<Contact> contactList = new ArrayList<Contact>();
        Session session = new HibernateUtil().getSession();
        try {
            String query = "from ContactImpl where id ='" + id + "' ";
            contactList = session.createQuery(query).list();
        } finally {
            session.close();
        }
        return contactList.get(0);
    }
}
